﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System
Partial Public Class Service
#Region "现金单_基础资料管理"
    Public Function _BILL_Cash_ManageBase(ByVal FSession As String, ByRef FCinf As FBaseCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_AddNewCustom
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCinf Is Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            Select Case FCinf.FType
                Case 0
                    '--客户
                    If FCinf.FName Is Nothing Then
                        ErrString = "参数错误"
                        Return False
                    End If
                    '--检查客户信息
                    sqlstr = String.Format("select * from Base_Cash where FName='{0}' and FType=0", FCinf.FName)
                    ds = New DataSet
                    If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                        Return False
                    End If
                    If ds.Tables(0).Rows.Count < 1 Then
                        '--没找到,新增客户
                        sqlstr = String.Format("INSERT INTO Base_Cash(FName,FCName,FType) values('{0}','{1}',0)", FCinf.FName, FCinf.FCName)
                        FCinf.id = ds.Tables(0).Rows(0).Item("id")
                        If DControl.ExecSQL(sqlstr, ErrString) = False Then
                            ErrString = String.Format("增加客户信息失败!{0}错误信息:{1}", vbCrLf, ErrString)
                            Return False
                        End If
                        Return True
                    Else
                        '--更新原有客户
                        sqlstr = String.Format("update Base_Cash set FName='{0}',FCName='{1}' output inserted.id where FName='{0}'", FCinf.FName, FCinf.FCName)
                        ds = New DataSet
                        If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                            Return False
                        End If
                        If ds.Tables(0).Rows.Count < 1 Then
                            ErrString = "未知错误"
                            Return False
                        End If
                        FCinf.id = ds.Tables(0).Rows(0).Item("id")
                        Return True
                    End If
                    
                Case 1
                    '--事由
                    '--新增
                    If FCinf.FSummary = Nothing Then
                        ErrString = "参数错误"
                        Return False
                    End If
                    If FCinf.FSummary.Trim = String.Empty Then
                        ErrString = "参数错误"
                        Return False
                    End If
                    sqlstr = String.Format("select * from Base_Cash where FSummary='{0}' and FType=1", FCinf.FSummary.Trim)
                    ds = New DataSet
                    If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                        ErrString = FControl.GetErrString("增加事由失败!", ErrString)
                        Return False
                    End If
                    If ds.Tables(0).Rows.Count < 1 Then
                        '--新增
                        sqlstr = String.Format("INSERT INTO Base_Cash(FSummary,FType) values('{0}',1)", FCinf.FSummary.Trim)
                        If DControl.ExecSQL(sqlstr, ErrString) = False Then
                            ErrString = FControl.GetErrString("增加事由失败!", ErrString)
                            Return False
                        End If
                        ds = New DataSet
                        sqlstr = String.Format("select * from Base_Cash where FSummary='{0}' and FType=1", FCinf.FSummary.Trim)
                        If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                            ErrString = FControl.GetErrString("取事由内码失败", ErrString)
                            Return False
                        End If
                        If ds.Tables.Count < 1 Then
                            ErrString = FControl.GetErrString("取事由内码失败!", "内部服务器错误")
                            Return False
                        End If
                        If ds.Tables(0).Rows.Count < 1 Then
                            ErrString = FControl.GetErrString("取事由内码失败!", "未知错误")
                            Return False
                        End If
                        FCinf.id = ds.Tables(0).Rows(0).Item("id")
                        Return True
                    Else
                        '--取内码
                        FCinf.id = ds.Tables(0).Rows(0).Item("id")
                        Return True
                    End If
                Case Else
                    '--错误
                    ErrString = "参数不正确"
                    Return False
            End Select
        Catch ex As Exception
            ErrString = FControl.GetErrString("操作失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_删除基础资料"
    Public Function _BILL_Cash_DelBase(ByVal FSession As String, ByVal FCinf As FBaseCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_DelBase
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If

        Try
            Dim sqlstr As String = String.Empty
            Dim ds As New DataSet
            Select Case FCinf.FType
                Case 0
                    '--客户
                    '-检查数据
                    sqlstr = "select * from Base_Cash where id=" & FCinf.id
                    ds = New DataSet
                    If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                        Return False
                    End If
                    If ds.Tables(0).Rows.Count < 1 Then
                        ErrString = "未找到指定的项目"
                        Return False
                    End If
                    '--检查引用
                    sqlstr = "select * from Bill_Cash where FCid=" & FCinf.id
                    ds = New DataSet
                    If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                        ErrString = FControl.GetErrString("删除客户失败!", ErrString)
                        Return False
                    End If
                    If ds.Tables(0).Rows.Count > 0 Then
                        ErrString = FControl.GetErrString("删除客户失败!", "客户已经有业务往来,不能删除!")
                        Return False
                    End If
                    sqlstr = "delete Base_Cash where id=" & FCinf.id
                    Return DControl.ExecSQL(sqlstr, ErrString)
                Case 1
                    '--事由
                    sqlstr = "delete Base_Cash where FType=1 and id=" & FCinf.id
                    Return DControl.ExecSQL(sqlstr)
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
        Catch ex As Exception
            ErrString = FControl.GetErrString("操作失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_取基础资料列表"
    Public Function _BILL_Cash_GetBaseList(ByVal FSession As String, ByVal FType As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_GetBaseList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            Select Case FType
                Case "Custom"
                    Select Case FStyle
                        Case "*"
                            sqlstr = "select * from Base_Cash where FType=0"
                        Case Else
                            ErrString = "参数错误"
                            Return False
                    End Select
                Case "Summary"
                    Select Case FStyle
                        Case "*"
                            sqlstr = "select id,FSummary from Base_Cash where FType=1"
                        Case Else
                            ErrString = "参数错误"
                    End Select
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = FControl.GetErrString("查询失败!", ErrString)
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("操作失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_取基础资料信息"
    Public Function _BILL_Cash_GetBaseInfo(ByVal FSession As String, ByRef FCinf As FBaseCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_GetBaseInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            Select Case FCinf.FType
                Case 0
                    '--客户
                    sqlstr = "select * from Base_Cash where FType=0 and id=" & FCinf.id
                Case 1
                    '--事由
                    sqlstr = "select * from Base_Cash where FType=1 and id=" & FCinf.id
            End Select
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                ErrString = FControl.GetErrString("读取信息失败!", ErrString)
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("读取信息失败", "未找到指定项目")
                Return False
            End If
            FCinf.FName = ds.Tables(0).Rows(0).Item("FName")
            FCinf.FCName = ds.Tables(0).Rows(0).Item("FCName")
            FCinf.FSummary = ds.Tables(0).Rows(0).Item("FSummary")
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("操作失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_保存"
    Public Function _BILL_Cash_SaveBill(ByVal FSession As String, ByRef FBinf As FBillCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_SaveBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = FControl.GetErrString("保存单据失败!", ErrString)
                Return False
            End If
            '--权限
            Dim FisNew As Boolean = _User_CheckAuth(FSession, "BILL", "FCBILLNEW", ErrString) '新单据权限
            Dim FVMY As Boolean = _User_CheckAuth(FSession, "BILL", "FCBILLVERIFYMY", ErrString) '审核自己的单据
            Dim FVOther As Boolean = _User_CheckAuth(FSession, "BILL", "FCBILLVERIFY", ErrString) '审核其它人的单据
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            If FBinf.id > 0 Then
                '--审核
                If FBinf.FUid = FUinf.id Then
                    '-自己的单据
                    If FVMY = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                Else
                    '--其它人的单据
                    If FVOther = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                End If
                '--查询单据是否存在
                sqlstr = "select FBillNo from Bill_Cash where id=" & FBinf.id
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("收费失败!", ErrString)
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("收费失败!", "未找到指定的项目")
                    Return False
                End If
                If FBinf.FBillNo <> ds.Tables(0).Rows(0).Item("FBillNo") Then
                    ErrString = FControl.GetErrString("收费失败!", "类型不匹配")
                    Return False
                End If
                '--检查分组
                Dim FUids As String() = Nothing
                If GetUserPair(FUinf.id, FUids, ErrString) = False Then
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                If FUids.Contains(FBinf.FUid) = False Then
                    ErrString = "与制单人不在同一组,无法执行收费操作!"
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                '--保存
                sqlstr = String.Format("UPDATE Bill_Cash set FVid={0},FVName='{1}' where id={2}", FUinf.id, FUinf.FRName, FBinf.id)
                FBinf.FVid = FUinf.id
                FBinf.FVName = FUinf.FRName
                Return DControl.ExecSQL(sqlstr, ErrString)
            Else
                '--新单
                If FisNew = False Then
                    ErrString = "没有权限"
                    Return False
                End If
                '--检查必填项
                If FBinf.FDate = Nothing Then
                    ErrString = "日期不能为空"
                    Return False
                End If
                If FBinf.FCustom.FName = Nothing Then
                    ErrString = "客户不能为空!"
                    Return False
                End If
                If FBinf.FCustom.id = 0 Then
                    ErrString = "客户不能为空!"
                    Return False
                End If
                If FBinf.FAmount = Nothing Then
                    ErrString = "金额不能为空!"
                    Return False
                End If
                FBinf.FCustom.FType = 0
                '--检查客户和摘要
                _BILL_Cash_ManageBase(FSession, FBinf.FCustom, ErrString)
                '-摘要
                Dim fcinf As FBaseCash = New FBaseCash With {.FSummary = FBinf.FSummary, .FType = 1}
                _BILL_Cash_ManageBase(FSession, fcinf, ErrString)
                FBinf.FUid = FUinf.id
                FBinf.FUName = FUinf.FRName
                '--单号,验证码
                Dim FBLSH As Integer = 0
                If FBinf.FBillNo Is Nothing Then
                    If GetBillMaxNumber(2, FUinf.FOrgid, FBLSH, ErrString) = False Then
                        ErrString = String.Format("取单据号失败!{0}错误信息:{1}", vbCrLf, ErrString)
                        Return False
                    End If
                    FBinf.FBillNo = FBinf.FDate.ToString("yyyyMMdd") & FBLSH.ToString("000000")
                    FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH.ToString("0000")) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
                End If
                If FBinf.FBCode Is Nothing Then
                    FBinf.FBCode = Guid.NewGuid.ToString.ToUpper
                End If
                '--开始保存
                sqlstr = String.Format("INSERT INTO Bill_Cash(FCid,FName,FCName,FBillNo,FVerifyCode,FDate,FRDate,FSummary,FMark,FUid,FUName,FBCode,FAmount,FOrgid) values('{0}','{1}','{2}','{3}','{4}','{5}',GETDATE(),'{6}','{7}','{8}','{9}','{10}','{11}',{12})", FBinf.FCustom.id, FBinf.FCustom.FName, FBinf.FCustom.FCName, FBinf.FBillNo, FBinf.FVerifyCode, FBinf.FDate.ToString, FBinf.FSummary, FBinf.FMark, FUinf.id, FUinf.FRName, FBinf.FBCode, FBinf.FAmount, FUinf.FOrgid)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = FControl.GetErrString("保存失败!", ErrString)
                    Return False
                End If
                '--取ID号
                sqlstr = String.Format("select id from Bill_Cash where FBCode='{0}'", FBinf.FBCode)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("取单据内码失败!", ErrString)
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = FControl.GetErrString("取单据内码失败!", "未知错误")
                    Return False
                End If
                FBinf.id = ds.Tables(0).Rows(0).Item("id")
                FBinf.FUid = FUinf.id
                FBinf.FUName = FUinf.FRName
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("保存单据失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "增加打印次数"
    Public Function _Bill_Cash_AddPrintCount(ByVal FSession As String, ByRef FBInf As FBillCash, ByRef ErrString As String) As Boolean Implements IService._Bill_Cash_AddPrintCount
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBInf Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBInf.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = "update Bill_Cash set FPCount=FPCount+1 output inserted.FPCount where FCancel=0 and id=" & FBInf.id
            Dim ds As New DataSet
            If FBInf.FVid <> FUinf.id Then
                '--打印其他人审核单据,检查权限
                ErrString = "只能打印自己收费的单据"
                Return False
            Else
                '--打印自己审核的单据
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败", "无法找到相应单据")
                Return False
            End If
            FBInf.FPCount = ds.Tables(0).Rows(0).Item("FPCount")
            Return True
        Catch ex As Exception
            FControl.GetErrString("打印失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_获取信息"
    Public Function _BILL_Cash_GetBillInfo(ByVal FSession As String, ByVal FBid As String, ByVal FBillNo As String, ByRef FBInfo As FBillCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_GetBillInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        If FBid.Trim = String.Empty And FBillNo.Trim = String.Empty Then
            ErrString = FControl.GetErrString("取单据信息失败!", "参数不正确")
            Return False
        End If
        Try
            Dim sqlstr As String = "select * from Bill_Cash"
            Dim ds As New DataSet
            If FBid.Trim <> String.Empty Then
                '--按ID查询
                sqlstr += " where id=" & FBid.Trim
            ElseIf FBillNo.Trim <> String.Empty Then
                '--按单据号查询
            Else
                FControl.GetErrString("取单据信息失败!", "参数不正确!")
                Return False
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "内部服务器错误!")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("取单据信息失败!", "未找到指定项目")
                Return False
            End If
            '--用户信息
            Dim FUInf As New FUserInfo
            If GetUserInfoBySession(FSession, FUInf, ErrString) = False Then
                ErrString = FControl.GetErrString("取单据信息失败!", ErrString)
                Return False
            End If
            '--检查权限
            If ds.Tables(0).Rows(0).Item("FUid") <> FUInf.id Then
                If _User_CheckAuth(FSession, "BILL", "FCBILLVIEW", "") = False Then
                    ErrString = "没有权限!"
                    Return False
                End If
            End If
            '--写数据项
            FBInfo.FCustom = New FBaseCash
            Dim r As DataRow = ds.Tables(0).Rows(0)
            FBInfo.id = FControl.cvN(r.Item("id"))
            FBInfo.FBillNo = FControl.cvN(r.Item("FBillNo"))
            FBInfo.FVerifyCode = FControl.cvN(r.Item("FVerifyCode"))
            FBInfo.FDate = FControl.cvN(r.Item("FDate"))
            FBInfo.FRDate = FControl.cvN(r.Item("FRDate"))
            FBInfo.FCustom.id = FControl.cvN(r.Item("FCid"))
            FBInfo.FCustom.FName = FControl.cvN(r.Item("FName"))
            FBInfo.FCustom.FCName = FControl.cvN(r.Item("FCName"))
            FBInfo.FSummary = FControl.cvN(r.Item("FSummary"))
            FBInfo.FAmount = FControl.cvN(r.Item("FAmount"))
            FBInfo.FUid = FControl.cvN(r.Item("FUid"))
            FBInfo.FUName = FControl.cvN(r.Item("FUName"))
            FBInfo.FVid = FControl.cvN(r.Item("FVid"))
            FBInfo.FVName = FControl.cvN(r.Item("FVName"))
            FBInfo.FCancel = FControl.cvN(r.Item("FCancel"))
            FBInfo.FPCount = FControl.cvN(r.Item("FPCount"))
            FBInfo.FBCode = FControl.cvN(r.Item("FBCode"))
            FBInfo.FMark = FControl.cvN(r.Item("FMark"))
            FBInfo.FOrgid = FControl.cvN(r.Item("FOrgid"))
            Return True
        Catch ex As Exception
            FControl.GetErrString("取单据信息失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_查询"
    Public Function _BILL_Cash_GetBillList(ByVal FSession As String, ByVal FSDate As Date, ByVal FEDate As Date, ByVal FName As String, ByVal FStyle As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_GetBillList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ConFUid As String = " and FUid="
            Dim ConFName As String = " and FName like"
            '--检查权限
            Dim FIsAuth As Boolean = _User_CheckAuth(FSession, "BILL", "FCBILLVIEW", "")
            If FIsAuth Then
                '--所有
                ConFUid = Nothing
            Else
                '--自己
                ConFUid += FUinf.id.ToString
            End If
            '--检查单位名
            If Not FName Is Nothing Then
                If FName.Trim <> String.Empty Then
                    ConFName = String.Format(" and FName like '%{0}'", FName.Trim)
                Else
                    ConFName = Nothing
                End If
            Else
                ConFName = Nothing
            End If
            '--开票单位
            Dim FOrg As String = ""
            If FUinf.id = 1 Then
                FOrg = ""
            Else
                '--过滤开票单位
                FOrg = " and FOrgid=" & FUinf.FOrgid
            End If
            DControl.ExecSQL("update t set t.FOrgName=t1.FName from Bill_Cash t,Base_Org t1 where t.FOrgid=t1.id", "")
            Select Case FStyle
                Case "*"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单据号',FVerifyCode '验证码',FDate '制单日期',FSummary '交款事由',FAmount '交款金额',FMark '备注',FUName '制单',FVName '收款人',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Cash where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "*ZF"
                    sqlstr = String.Format("select id '内码',FName '交款单位',FCName '交款人',FBillNo '单据号',FVerifyCode '验证码',FDate '制单日期',FSummary '交款事由',FAmount '交款金额',FMark '备注',FUName '制单',FVName '收款人',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Cash where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CN"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Cash where FDate BETWEEN '{0}' and '{1}'{2}{3}{4}", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case "CNZF"
                    sqlstr = String.Format("select id,FBillNo '单号',FVerifyCode '验证码',FName '交款单位',FCName '交款人',FDate '日期',FAmount '金额',FUName '制单人',FVName '收费',FPCount '打印次数',case FCancel when 0 then '正常' else '作废' end '状态',FOrgName '收款单位' from Bill_Cash  where FDate BETWEEN '{0}' and '{1}'{2}{3}{4} and FCancel=0", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"), ConFUid, ConFName, FOrg)
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            '--查询 
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("查询单据失败!", ErrString)
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = FControl.GetErrString("查询时出现错误!", ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "现金单_修改"
    Public Function _BILL_Cash_ModifyBill(ByVal FSession As String, ByVal FBinf As FBillCash, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_ModifyBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBinf.id <= 0 Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBinf.FVid > 0 Then
                ErrString = "不能修改已收费的单据!"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            If FBinf.FUid <> FUinf.id Then
                ErrString = "只能修改自己填制的单据!"
                Return False
            End If
            '--检查必填项
            If FBinf.FDate = Nothing Then
                ErrString = "日期不能为空"
                Return False
            End If
            If FBinf.FCustom.FName = Nothing Then
                ErrString = "客户不能为空!"
                Return False
            End If
            If FBinf.FCustom.id = 0 Then
                ErrString = "客户不能为空!"
                Return False
            End If
            If FBinf.FAmount = Nothing Then
                ErrString = "金额不能为空!"
                Return False
            End If
            FBinf.FCustom.FType = 0
            '--检查客户和摘要
            _BILL_Cash_ManageBase(FSession, FBinf.FCustom, ErrString)
            '-摘要
            _BILL_Cash_ManageBase(FSession, New FBaseCash With {.FSummary = FBinf.FSummary, .FType = 1}, ErrString)
            FBinf.FUid = FUinf.id
            FBinf.FUName = FUinf.FRName
            '--单号,验证码
            Dim FBLSH As Integer = Right(FBinf.FBillNo, 4)
            '-计算验证码

            FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
            Dim sqlstr As String = String.Format("update Bill_Cash set FCid={0},FName='{1}',FCName='{2}',FDate='{3}',FSummary='{4}',FAmount={5},FMark='{6}',FVerifyCode='{7}' where id={8}", FBinf.FCustom.id, FBinf.FCustom.FName, FBinf.FCustom.FCName, FBinf.FDate, FBinf.FSummary, FBinf.FAmount, FBinf.FMark, FBinf.FVerifyCode, FBinf.id)
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                Return False
            End If
            DControl.WriteSysLog("BILL", FUinf.id, FUinf.FName, "修改单据", "单据类型:现金收据 单号: " & FBinf.FBillNo, 0)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region
#Region "现金单_作废"
    Public Function _BILL_Cash_BillCancel(ByVal FSession As String, ByVal FBid As Integer, ByRef ErrString As String) As Boolean Implements IService._BILL_Cash_BillCancel
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBid = Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = String.Format("取用户信息失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            '--取单据信息
            Dim sqlstr As String = "select * from Bill_Cash where id=" & FBid
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "未找到指定单据"
                Return False
            End If
            Dim FBillNo As String = ds.Tables(0).Rows(0).Item("FBillNo")
            If FControl.cvN(ds.Tables(0).Rows(0).Item("FVid")) <> Nothing And FUinf.id <> 1 Then
                ErrString = "只有管理员可以作废已收费的单据!"
                Return False
            End If
            If ds.Tables(0).Rows(0).Item("FUid") = FUinf.id Then
                '--自己的单据
                If _User_CheckAuth(FSession, "BILL", "FCBILLCANCEL", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            Else
                '--其它人的单据
                If _User_CheckAuth(FSession, "BILL", "FCBILLCANCELA", ErrString) = False Then
                    ErrString = "没有权限"
                    Return False
                End If
            End If
            '--权限
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = String.Format("作废单据失败!{0}错误信息:无法找到指定的记录", vbCrLf)
                Return False
            End If
            '--开始作废
            sqlstr = "update Bill_Cash set FCancel=-1 where id=" & FBid
            If DControl.ExecSQL(sqlstr, ErrString) = False Then
                ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ErrString)
                Return False
            End If
            '--写日志
            DControl.WriteSysLog("BILL", FUinf.id, FUinf.FRName, "作废单据", "作废商品销售收据 单号:" & FBillNo, 1)
            Return True
        Catch ex As Exception
            ErrString = String.Format("作废单据失败!{0}错误信息:{1}", vbCrLf, ex.Message)
            Return False
        End Try
    End Function
#End Region
End Class
